import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import Login from '../components/Login.vue'

import CategoryEdit from '../components/CategoryEdit.vue'
import CategoryList from '../components/CategoryList.vue'

import ItemEdit from '../components/ItemEdit.vue'
import ItemList from '../components/ItemList.vue'

import HeroEdit from '../components/HeroEdit.vue'
import HeroList from '../components/HeroList.vue'

import ArticleEdit from '../components/ArticleEdit.vue'
import ArticleList from '../components/ArticleList.vue'

import AdEdit from '../components/AdEdit.vue'
import AdList from '../components/AdList.vue'

import AdminUserEdit from '../components/AdminUserEdit.vue'
import AdminUserList from '../components/AdminUserList.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/login', name: 'login', component: Login, meta: { isPublic: true } },
  {
    path: '/',
    name: 'main',
    component: Main,
    children: [
      { path: '/categories/create', component: CategoryEdit },
      { path: '/categories/edit/:id', component: CategoryEdit, props: true },
      { path: '/categories/list', component: CategoryList },

      { path: '/items/create', component: ItemEdit },
      { path: '/items/edit/:id', component: ItemEdit, props: true },
      { path: '/items/list', component: ItemList },

      { path: '/heroes/create', component: HeroEdit },
      { path: '/heroes/edit/:id', component: HeroEdit, props: true },
      { path: '/heroes/list', component: HeroList },

      { path: '/articles/create', component: ArticleEdit },
      { path: '/articles/edit/:id', component: ArticleEdit, props: true },
      { path: '/articles/list', component: ArticleList },

      { path: '/ads/create', component: AdEdit },
      { path: '/ads/edit/:id', component: AdEdit, props: true },
      { path: '/ads/list', component: AdList },

      { path: '/admin_user/create', component: AdminUserEdit },
      { path: '/admin_user/edit/:id', component: AdminUserEdit, props: true },
      { path: '/admin_user/list', component: AdminUserList }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to.meta)
  if (!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  } else {
    next()
  }
})

export default router
